package com.chengcang.api.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chengcang.api.modules.sys.dao.SysUserTenantDao;
import com.chengcang.api.modules.sys.dto.SysUserTenantDTO;
import com.chengcang.api.modules.sys.entity.SysUserTenantEntity;
import com.chengcang.api.modules.sys.entity.UserTenant;
import com.chengcang.api.modules.sys.service.SysDictDataService;
import com.chengcang.api.modules.sys.service.SysUserTenantService;
import com.chengcang.common.enums.DataTypeEnum;
import com.chengcang.common.service.impl.CrudServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 用户租户中间表
 *
 * @author zhanli
 * @since 1.0.0 2023-01-06
 */
@Service
@RequiredArgsConstructor
public class SysUserTenantServiceImpl extends CrudServiceImpl<SysUserTenantDao, SysUserTenantEntity, SysUserTenantDTO> implements SysUserTenantService {

    private final SysDictDataService sysDictDataService;

    @Override
    public QueryWrapper<SysUserTenantEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<SysUserTenantEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public List<UserTenant> getUserTenantList(Long userId) {
        List<UserTenant> userTenants = baseDao.selectUserTenant(userId);
        for (UserTenant userTenant : userTenants) {
            userTenant.setUserTypeName(sysDictDataService.getValue(DataTypeEnum.USER_TYPE, String.valueOf(userTenant.getUserType())));
        }
        return userTenants;
    }


}